package classroom_service

import (
	"zhanyan/models"
	"zhanyan/pac/util"
	"zhanyan/service/seat_service"
)

// Classroom 教室
type Classroom struct {
	ID        int    `json:"id";gorm:"primary_key"`
	Name      string `json:"name"`
	Type      string `json:"type"`
	Location  string `json:"location"`
	TeacherID int
	Seat      []seat_service.Seat
}

func (c *Classroom) Add() error {
	check, err := models.CheckClassroom(c.Name)
	if check != nil {
		return err
	}
	classroom := models.Classroom{}
	err = util.Transfer(c, &classroom)
	if err != nil {
		return err
	}
	id, err := models.AddClassroom(&classroom)
	if err != nil {
		return err
	}
	c.ID = id
	return nil
}

func (c *Classroom) Update() error {
	_, err := models.CheckClassroomByID(c.ID)
	if err != nil {
		return err
	}
	check, err := models.CheckClassroom(c.Name)
	if check != nil && check.ID != c.ID {
		return util.ClassroomExistedError
	}
	classroom := models.Classroom{}
	err = util.Transfer(c, &classroom)
	if err != nil {
		return err
	}
	err = models.UpdatedClassroom(&classroom)
	if err != nil {
		return err
	}
	return nil
}

func (c *Classroom) Delete() error {
	_, err := models.CheckClassroomByID(c.ID)
	if err != nil {
		return err
	}
	seats, err := models.QuerySeats(c.ID)
	if err != nil {
		return err
	}
	ids := []int{}
	for _, s2 := range *seats {
		ids = append(ids, s2.ID)
	}
	if err != nil {
		return err
	}
	err = models.DeleteSeats(ids)
	if err != nil {
		return err
	}
	err = models.DeleteClassroom(c.ID)
	if err != nil {
		return err
	}
	return nil
}

func (c *Classroom) Get() (*models.Classroom, error) {
	_, err := models.CheckClassroomByID(c.ID)
	if err != nil {
		return nil, err
	}
	classroom, err := models.QueryClassroomAndSeats(c.ID)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetAll() (*[]models.Classroom, error) {
	classroom, err := models.QueryAllClassroom()
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetAllNumber() (int64, error) {
	num, err := models.QueryAllClassroomNumber()
	if err != nil {
		return 0, err
	}
	return num, nil
}

func (c *Classroom) GetAllPage(pageSize int, pageCode int) (*[]models.Classroom, error) {
	classroom, err := models.QueryClassroomPage(pageSize, pageCode)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassByName() (*[]models.Classroom, error) {
	classroom, err := models.QueryAllClassroomByName(c.Name)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassByNameNumber() (int64, error) {
	num, err := models.QueryAllClassroomByNameNumber(c.Name)
	if err != nil {
		return 0, err
	}
	return num, nil
}

func (c *Classroom) GetClassByNamePage(pageSize int, pageCode int) (*[]models.Classroom, error) {
	classroom, err := models.QueryClassroomPageByName(c.Name, pageSize, pageCode)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassByTidPage(pageSize int, pageCode int) (*[]models.Classroom, error) {
	classroom, err := models.QueryClassroomPageByTid(c.TeacherID, pageSize, pageCode)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassByTidNum() (int64, error) {
	num, err := models.QueryClassroomByTidNumber(c.TeacherID)
	if err != nil {
		return 0, err
	}
	return num, nil
}

func (c *Classroom) GetClassByNameAndTid() (*[]models.Classroom, error) {
	classroom, err := models.QueryClassroomByNameAndTid(c.Name, c.TeacherID)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassPageByNameAndTid(ps, pc int) (*[]models.Classroom, error) {
	classroom, err := models.QueryClassroomPageByNameAndTid(c.Name, c.TeacherID, ps, pc)
	if err != nil {
		return nil, err
	}
	return classroom, nil
}

func (c *Classroom) GetClassByNameAndTidNumber() (int64, error) {
	num, err := models.QueryClassroomByNameAndTidNum(c.Name, c.TeacherID)
	if err != nil {
		return 0, err
	}
	return num, nil
}
